<?php
require_once 'BaseController.php';
class Admin_TypeController extends BaseController
{

    public function init(){
          Zend_Loader::loadClass('AuthModel');
          Zend_Loader::loadClass('ParentTypeModel');
          Zend_Loader::loadClass('testModel');
    }
    
    public function indexAction(){
        if(isset($_POST['data'])){
            $parent = $_POST['data'];
            $this->view->parent = $parent;
        }
		$dt = new ParentTypeModel();
		$requestnew = $this->_request;
		$request = $this->getRequest();
		if ($this->_request->isPost()){
			$id = $request->getParam('id');
			$name = $request->getParam('name');
			$description = $request->getParam('description');
            if($name != ""){
				if((trim($name) != "")){
					
						   $data = array("name" => $name,
						                 "description" => $description,
                                         "status" => '1',
                                         );
						   if($requestnew->getParam('submit') == 'ADD'){
						   		$test = $dt->getTypeByName($name);
						   		$count_test = count($test);
						   		if($count_test == 0) $dt->insert($data);
						    	else echo "<br><br><p align= 'center' style='color:red;'>Tên thể loại phim bạn nhập vào dã có, xin vui lòng kiêm tra lại. </p>";
					       }
					       else if($requestnew->getParam('submit') == 'EDIT'){
    	 	  	                 $dt->update($data,"id=".$id);
                           }
					 
				}
				else echo "<br><br><p align= 'center' style='color:red;'>Ô Name là không được trống. Xin vui lòng kiểm tra lỗi</p>";
			}
		}
		$this->view->datas = $dt->getListDB();
		
	}
	
	public function deleteAction(){
		$dt = new ParentTypeModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		if($id){
			$dt->delete("id=".$id);
		}
		exit();
	}
	
    public function editAction(){
    	$layout = $this->_helper->layout();
		$layout->disableLayout();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$dt = new ParentTypeModel();
		$this->view->data = $dt->getTypeById($id);
	}
	 
	public function editstatusAction(){
		$layout = $this->_helper->layout();
		$layout->disableLayout();
		$dt = new ParentTypeModel();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$status = $request->getParam('status');
		if($id){
			if($status == 1) $status=0;
			else if($status ==0) $status=1;
			$data = array("status" => $status);
			$dt->update($data,"id=".$id);
			$this->view->data = $dt->getTypeById($id);
		}
		
	}
    /////////////////////////////////////
    public function categoryAction(){
        $cate_id = $_REQUEST['id'];
        $model = new ParentTypeModel();
        $category = $model->getTypeById($cate_id);
        $name_types = $category[0]['name_types'];
              
        $explode_types = explode('|',$name_types);
        $max_ep = 0;
        for($i = 0;$i < count($explode_types); $i++){
            $explode_types[$i] = explode('-',$explode_types[$i]);
            if(count($explode_types[$i]) == 1){
                $explode_types[$i][1] = $explode_types[$i][0];
                $explode_types[$i][0] = $i;
                $max_ep = $i;
            }else{
                if(intval($explode_types[$i][0]) > $max_ep ) $max_ep = $explode_types[$i][0];
            }
        };
        $this->view->listtype = $explode_types;
        
        
        if ($this->_request->isPost()){
			$old_ep = intval($this->_request->getParam('old_ep'));
            $del_ep = intval($this->_request->getParam('del_ep'));
            $add_ep = intval($this->_request->getParam('add_ep'));
            $num_type = count($explode_types) + $add_ep;
            for($i = 0 ;$i < $num_type ;$i++){
                if($this->_request->getParam('ep-'.$i) != '' && $this->_request->getParam('ep-'.$i)!= null){
                    if($add_ep != 0 && $i >= $old_ep){
                        $explode_types[$i]['0'] = $max_ep+$i-$old_ep+1;//phep tinh nay lay ra so thu tu can dien cho type moi
                    };   
                    $name_type[$i] =$explode_types[$i]['0']."-".$this->_request->getParam('ep-'.$i);
                }
            };
            $name_types = implode('|',$name_type);
            $data= array(
                'name_types' => $name_types,
            );
            
            $model->update($data, "id=".$cate_id);
            $this->_redirect('/admin/type/category?id='.$cate_id);
        }
        
    }
    public function sorttypeAction(){
        $this->view->headLink()->appendStylesheet("/css/jquery-ui.css");
        $this->view->headScript()->appendFile("/js/jquery-ui-1.9.1.custom.min.js");
        //      Xu ly du lieu
		$dt = new ParentTypeModel();

		if($this->_request->isPost()){
			$request = $this->getRequest();
			$chuoi = $request->getParam('sort-menu');
			$sort = explode(",", $chuoi);
			$n= count($sort);
            if(isset($_REQUEST['id'])){
                $id = $_REQUEST['id'];
                $datas = $dt->getTypeById($id);
                $datas = $datas[0]['name_types'];
                $datas = explode('|',$datas);
                $name_types = "";
                for($i = 0; $i< $n;$i++){
                    $sort[$i] = (int)$sort[$i];
                    if($name_types != ""){
                        $name_types =$name_types."|";   
                    }
                    $name_types = $name_types."".$datas[$sort[$i]];
                }
                $data = array("name_types" => $name_types);
                $dt->update($data, "id=".$id);
            }else{
                for($i = 0 ; $i < $n;$i++){
    				$dem = $i +1;
    				$sort[$i] = (int)$sort[$i];
    				$data = array("sort" => $dem);
    				$dt->update($data, "id=".$sort[$i]);
    			}
            }
		}
        if(isset($_REQUEST['id'])){
            $id = $_REQUEST['id'];
            $datas = $dt->getTypeById($id);
            $datas = $datas[0]['name_types'];
            $datas = explode('|',$datas);
            for($i=0;$i<count($datas);$i++){
                $arr[]= array(
                    'id'=>$i,
                    'name'=>$datas[$i]
                );
            }
            $this->view->datas = $arr;
        }else $this->view->datas = $dt->getListType();
    }
}
    